from flask import Blueprint,render_template,g,redirect,request,url_for,jsonify
from app import db
from app.models import Product,User,Updatejour,Group,Acctproduct
from app.method.web_methods import getmember
import time

all_server_page = Blueprint('all_server_page',__name__,template_folder='templates')
@all_server_page.route('/all-server',methods = ['GET','POST'])
def all_server():
    if g.user is not None and g.user.is_authenticated:
        server_lists = db.session.query(Product.server_name, Product.vmware_name, Product.server_config,
                                                Product.server_type, Product.server_address, Product.members,
                                                Product.creat_date, Product.creat_person, Product.id).filter().all()
        pro_num = len(server_lists)
        hs_current_id = str(g.user).replace("'", "")
        hs_current_user = db.session.query(User.username).filter_by(id = hs_current_id).first()[0]
        mes_lists = db.session.query(Updatejour.output,Updatejour.update_date,Updatejour.update_time,Updatejour.message,Updatejour.id,Updatejour.log_path).filter_by(read_status = 1,up_persion=hs_current_user).all()
        mes_len = len(mes_lists)
        if request.method == 'POST':

            if request.values.get('fun')=='getgroup':
                get_big_group = request.values.get('big_group')
                small_group_lists = db.session.query(Group.small_group).filter_by(big_group = get_big_group).all()

                small_list = ''
                for small in small_group_lists:
                    if small_group_lists.index(small) == 0:
                        small_list = small_list + small[0]
                    else:
                        small_list = small_list + ' ' + small[0]
                return small_list

            elif request.values.get('fun')=='getserver':
                get_small = request.values.get('small_group')
                if get_small == '请选择':
                    server_lists = db.session.query(Product.server_name, Product.vmware_name, Product.server_config,
                                                    Product.server_type, Product.server_address, Product.members,
                                                    Product.creat_date, Product.creat_person, Product.id).filter().all()
                else:
                    server_lists = db.session.query(Product.server_name, Product.vmware_name, Product.server_config,
                                                Product.server_type, Product.server_address, Product.members,
                                                Product.creat_date, Product.creat_person, Product.id).filter_by(group = get_small).all()

            elif request.values.get('fun')=='getmember':
                server_ip = request.values.get('serverip')
                members = getmember(server_ip)
                return members

            elif request.values.get('fun')=='addmember':
                select_member = request.form.getlist('select_member')
                host = request.values.get('host')

                for member in select_member:
                    old_members = db.session.query(Product.members, Product.server_name,
                                                   Product.server_address).filter_by(server_address=host).all()
                    if old_members[0][0] != ' ':
                        new_members = old_members[0][0]+','+member
                    else:
                        new_members = member
                    u = Product.query.filter_by(server_address=host).first()
                    u.members = new_members
                    hs_id = db.session.query(User.id).filter_by(username = member).all()
                    hs_acct_obj = Acctproduct(server_name=old_members[0][1], server_owner_id=hs_id[0][0],
                                              server_address=host)
                    db.session.add(hs_acct_obj)
                    db.session.add(u)
                    db.session.commit()
                return redirect('/all-server')


    else:
        return redirect('/login')
    return render_template('hs-all-server.html', server_lists=server_lists, pro_num=pro_num, hs_current_user=hs_current_user, mes_lists=mes_lists, mes_len=mes_len)
